package com.zimi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.SysUser;
import com.zimi.model.WfTask;
import com.zimi.model.WfTaskVO;
import com.zimi.service.WfTaskService;
import com.zimi.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("流程代办")
@RestController
@RequestMapping("wf/task")
public class WfTaskApi {

    @Autowired
    private WfTaskService service;

    @PostMapping("list")
    @ApiOperation("查询所有")
    public List<WfTask> selectAll(WfTask record) {
        return service.selectAll(record);
    }

    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public WfTask selectByPrimaryKey(@PathVariable("id") int id) {
        return service.selectByPrimaryKey(id);
    }

    @PostMapping("del/{id}")
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(@PathVariable("id") int id) {
        return service.deleteByPrimaryKey(id) > 0 ? true : false;
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public boolean insert(WfTask record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PostMapping("update")
    @ApiOperation("根据条件修改信息")
    public boolean updateByPrimaryKey(WfTask record) {
        return service.updateByPrimaryKeySelective(record) > 0 ? true : false;
    }

    @PostMapping("myWfTask")
    @ApiOperation("代办工作")
    public Map getMyWfTask(WfTask record, int page, int limit) {
        PageHelper.startPage(page, limit);
        SysUser user = UserUtil.getUser();
        record.setTaskExecutor(user.getId());
        record.setWfTaskState(1);
        List<WfTaskVO> list = service.selectMyWfTask(record);
        PageInfo<WfTaskVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "");
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }


}
